﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxTabControl;
using DevExpress.Web.ASPxEditors;
using System.Data;
using DevExpress.Web.ASPxDataView;
using Touchmax;

public partial class OrderEdit :Page
{
    protected void Page_Init(object sender, EventArgs e)
    {
            string on = Request.QueryString["no"] ?? "0";
        if (!IsPostBack)
        {
            Session["VehicleWhere"] = null;
            int no = SafeValue.SafeInt(on, 0);
            string sn = Request.QueryString["sn"] ?? "";
            if (no > 0)
            {
                Session["VehicleWhere"] = "SequenceId='" + no + "'";
            }
            else if(no==0&&sn!="")
            {
                this.grd_Cont.AddNewRow();
            }

        }
        
        if (Session["VehicleWhere"] != null)
        {

            this.dsVehicle.FilterExpression = Session["VehicleWhere"].ToString();
            if (this.grd_Cont.GetRow(0) != null)
                this.grd_Cont.StartEdit(0);
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }

     #region Vehicle
    private void Save()
    {
        try
        {
            ASPxTextBox orderNo = this.grd_Cont.FindEditFormTemplateControl("txt_Oid") as ASPxTextBox;

            string refN = orderNo.Text;
            Wilson.ORMapper.OPathQuery query = new Wilson.ORMapper.OPathQuery(typeof(Touchmax.OpVehicle), "SequenceId='" + refN + "'");
            Touchmax.OpVehicle jo = Touchmax.Manager.ORManager.GetObject(query) as Touchmax.OpVehicle;
            bool isNew = false;
            if (jo == null)
            {
                jo = new Touchmax.OpVehicle();
                isNew = true;
                jo.JobOrder = Request.QueryString["sn"] ?? "";
                jo.UserId = HttpContext.Current.User.Identity.Name;
                jo.EntryDate = DateTime.Now;
            }
            
             ASPxComboBox yearCtr= this.grd_Cont.FindEditFormTemplateControl("cmb_Year") as ASPxComboBox;
             jo.Year = SafeValue.SafeInt(yearCtr.Text, 0);

             ASPxComboBox color1 = this.grd_Cont.FindEditFormTemplateControl("cmb_Color1") as ASPxComboBox;
             jo.Color1 = color1.Text;
             ASPxComboBox color2 = this.grd_Cont.FindEditFormTemplateControl("cmb_Color2") as ASPxComboBox;
             jo.Color2 = color2.Text;
             ASPxComboBox make = this.grd_Cont.FindEditFormTemplateControl("cmb_Make") as ASPxComboBox;
             jo.VehicleType = make.Text;
             ASPxTextBox colorCode1 = this.grd_Cont.FindEditFormTemplateControl("txt_ColorCode1") as ASPxTextBox;
            jo.ColorCode1 = colorCode1.Text;
            ASPxTextBox colorCode2 = this.grd_Cont.FindEditFormTemplateControl("txt_ColorCode2") as ASPxTextBox;
            jo.ColorCode2 = colorCode2.Text;

            ASPxComboBox vehicleModel = this.grd_Cont.FindEditFormTemplateControl("cmb_Model") as ASPxComboBox;
             jo.VehicleModel = vehicleModel.Text;

             ASPxComboBox formula1 = this.grd_Cont.FindEditFormTemplateControl("cmb_Formula1") as ASPxComboBox;
             jo.Formula1 = formula1.Text;
             ASPxComboBox formula2 = this.grd_Cont.FindEditFormTemplateControl("cmb_Formula2") as ASPxComboBox;
             jo.Formula2 = formula2.Text;
             ASPxTextBox stock = this.grd_Cont.FindEditFormTemplateControl("txt_Stock") as ASPxTextBox;
            jo.VehicleNo = stock.Text;
            ASPxTextBox vin = this.grd_Cont.FindEditFormTemplateControl("txt_Vin") as ASPxTextBox;
            jo.Vin = vin.Text;

            ASPxComboBox jobStatus = this.grd_Cont.FindEditFormTemplateControl("cmb_JobStatus") as ASPxComboBox;
            jo.Status = jobStatus.Text;
            


           if(isNew)
           {
                Touchmax.Manager.ORManager.StartTracking(jo, Wilson.ORMapper.InitialState.Inserted);
                Touchmax.Manager.ORManager.PersistChanges(jo);
            }
            else
            {
                Touchmax.Manager.ORManager.StartTracking(jo, Wilson.ORMapper.InitialState.Updated);
                Touchmax.Manager.ORManager.PersistChanges(jo);
            }
            if (isNew)
                Session["VehicleWhere"] = "SequenceId='" + jo.SequenceId+ "'";
                this.dsVehicle.FilterExpression = Session["VehicleWhere"].ToString();

                // orderNo.Text = refN;
                if (this.grd_Cont.GetRow(0) != null)
                    this.grd_Cont.StartEdit(0);
            }catch (Exception ex)
        { throw new Exception(ex.Message); }
    }
    private string GetNo(string noType)
    {
        string sql = "select Counter from XXSetup where Category='" + noType + "'";
        int cnt = SafeValue.SafeInt(Touchmax.Manager.ORManager.ExecuteScalar(sql), 0) + 1;

        return cnt.ToString();

    }
    private void SetNo(string no,string noType)
    {
        string sql = string.Format("update XXSetup set counter='{0}' where category='{1}'",no, noType);
       int res= Touchmax.Manager.ORManager.ExecuteCommand(sql);
    }

    
    protected void grd_Cont_Init(object sender, EventArgs e)
    {
        ASPxGridView grd = sender as ASPxGridView;
        if (grd != null)
        {
            grd.ForceDataRowType(typeof(Touchmax.OpVehicle));
        }
    }
    protected void grd_Cont_InitNewRow(object sender, DevExpress.Web.Data.ASPxDataInitNewRowEventArgs e)
    {
    }
   protected void grd_Cont_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
    {
        string s = e.Parameters;
         if (s == "Save")
        {
            Save();
        }
    }
   #endregion
    #region Estimate det
    protected void grid_InvDet_Init(object sender, EventArgs e)
    {
        ASPxGridView grd = sender as ASPxGridView;
        if (grd != null)
            grd.ForceDataRowType(typeof(OpInvoiceDet));
    }
    protected void grid_InvDet_BeforePerformDataSelect(object sender, EventArgs e)
    {
        ASPxGridView grd = (sender as ASPxGridView) as ASPxGridView;
        this.dsInvoiceDet.FilterExpression = "DocType='Estimate' and ItemId='" + SafeValue.SafeString(grd.GetMasterRowKeyValue(), "0") + "'";
    }
    protected void grid_InvDet_InitNewRow(object sender, DevExpress.Web.Data.ASPxDataInitNewRowEventArgs e)
    {
        e.NewValues["DocLineNo"] = 0;
        e.NewValues["TotAmt"] = 0;
    }
    protected void grid_InvDet_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
    {
        ASPxGridView grd = sender as ASPxGridView;
        //ASPxSpinEdit item = grd.FindEditFormTemplateControl("spin_det_ItemId") as ASPxSpinEdit;

        e.NewValues["ItemId"] = SafeValue.SafeString(grd.GetMasterRowKeyValue(), "0");
    }
    protected void grid_InvDet_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
    {
        ASPxGridView grd = (sender as ASPxGridView) as ASPxGridView;
        string sql = "select count(DocLineNo) from invoicedet where DocType='Estimate' and ItemId='" + SafeValue.SafeString(grd.GetMasterRowKeyValue(), "0") + "'";
        int lineN = SafeValue.SafeInt(Touchmax.Manager.ORManager.ExecuteScalar(sql), 0) + 1;
       e.NewValues["ItemId"] = SafeValue.SafeString(grd.GetMasterRowKeyValue(), "0");
        e.NewValues["DocType"] = "Estimate";
        sql = "select JobOrder from VehicleInfo where SequenceId='" + SafeValue.SafeString(grd.GetMasterRowKeyValue(), "0") + "'";
        e.NewValues["JobOrder"] = SafeValue.SafeString(Touchmax.Manager.ORManager.ExecuteScalar(sql), "") ;
    }
    protected void grid_InvDet_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
    {
    }

    #endregion
    #region select
    protected void gridPopCont_CustomJSProperties(object sender, ASPxGridViewClientJSPropertiesEventArgs e)
    {
        ASPxGridView grid = sender as ASPxGridView;
        object[] branchs = new object[grid.VisibleRowCount];
        object[] keyValues = new object[grid.VisibleRowCount]; ;
        for (int i = 0; i < grid.VisibleRowCount; i++)
        {
            keyValues[i] = grid.GetRowValues(i, "ItemID");
            branchs[i] = 0; // grid.GetRowValues(i, "FixedChgA");
        }
        e.Properties["cpAmt"] = branchs;
        e.Properties["cpKeyValues"] = keyValues;
    }
    #endregion


}
